﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;

using Maticsoft.Common;


namespace DQYT.HTDZ.CommonFactory
{
    public class CompositionFactory
    {
        /// <summary>
        /// 创建对象或从缓存获取
        /// 使用方法:从数据库中读取对应评价的所在的程序集AssemblyPath和完整类名ClassNamespace
        /// 如:
        /// IEvaluationService standardService = 
        ///     CompositionFactory.CreateObject<IEvaluationService>("DQYT.HTDZ.SinglewellEvaluationService", "DQYT.HTDZ.SinglewellEvaluationService.StandardEvaluationService");
        /// </summary>
        /// <typeparam name="T">接口类型，如 IEvaluationService</typeparam>
        /// <param name="AssemblyPath">程序集名称，如：DQYT.HTDZ.CommonFactory</param>
        /// <param name="ClassNamespace">类完整名称，如：DQYT.HTDZ.CommonFactory.CompositionFactory</param>
        /// <returns></returns>
        public static T CreateObject<T>(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                DataCache.SetCache(ClassNamespace, objType);// 写入缓存
            }
            return (T)objType;
        }
    }
}
